package com.henu.engineer.config

import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.HandlerExceptionResolver
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

/**
 * @author noexcept
 * @date 2021/7/23 0:27
 */
@Configuration
class WebConfig : WebMvcConfigurer {
    /**
     * 拦截所有除登录以外的请求，
     * 对登陆以外的请求进行验证。
     * @param registry
     * @see RequestInterceptor.preHandle
     * @see com.henu.engineer.controller.LoginController.login
     */
    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(RequestInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/login")
            .excludePathPatterns("/index.html")
            .excludePathPatterns("/")
    }

    /**
     * 设置主页
     */
    override fun addViewControllers(registry: ViewControllerRegistry) {
        registry.addViewController("/").setViewName("forward:index.html")
    }

    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**").allowedOrigins("*")
            .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE","PATCH")
            .allowCredentials(false).maxAge(3600)
    }


}